"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const config_1 = require("../../config");
const UserSer_1 = __importDefault(require("../../services/UserSer"));
const infoerr_1 = require("../middleware/infoerr");
const ImgSer_1 = __importDefault(require("../../services/ImgSer"));
exports.default = {
    login(req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = yield UserSer_1.default.login(req.body);
            if (data instanceof Object) {
                res.cookie("feifan", {
                    name: data.name,
                    id: data.id,
                    imgUrl: data.imgUrl,
                    right: data.right,
                }, { maxAge: config_1.tokenTime * 1000, httpOnly: false, signed: true });
                return (0, infoerr_1.info)(res, "登录成功", data);
            }
            if (!data)
                return (0, infoerr_1.err)(res, "账号或密码错误");
        });
    },
    registry(req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = yield UserSer_1.default.registry(req.body);
            if (typeof data === "string")
                return (0, infoerr_1.err)(res, data);
            if (data instanceof Object) {
                res.cookie("feifan", {
                    name: data.name,
                    id: data.id,
                    imgUrl: "/img/空.png",
                    right: data.right,
                }, { maxAge: config_1.tokenTime * 1000, httpOnly: false, signed: true });
                return (0, infoerr_1.info)(res, "注册成功", data);
            }
        });
    },
    isname(req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            if (typeof req.query.name === "string") {
                const is = yield UserSer_1.default.isname(req.query.name);
                return (0, infoerr_1.info)(res, is);
            }
        });
    },
    userimg(req, res) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            req.query.fileurl = req.query.fileurl.replace(/img/g, "upload");
            console.log(req.signedCookies.feifan.name, "上传了:", (_a = req.file) === null || _a === void 0 ? void 0 : _a.originalname, "位置:", req.query.fileurl);
            const data = yield UserSer_1.default.userupdate({
                imgUrl: req.query.fileurl,
            }, req.signedCookies.feifan.id);
            (0, infoerr_1.info)(res, "修改成功", { url: req.query.fileurl, status: data });
        });
    },
    articleimg(req, res) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            req.query.fileurl = req.query.fileurl.replace(/img/g, "upload");
            console.log(req.signedCookies.feifan.name, "上传了:", (_a = req.file) === null || _a === void 0 ? void 0 : _a.originalname, "位置:", req.query.fileurl);
            (0, infoerr_1.info)(res, "修改成功", { url: req.query.fileurl });
        });
    },
    theftarticleimg(req, res, next) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            try {
                req.query.fileurl = req.query.fileurl.replace(/img/g, "upload");
                console.log(req.signedCookies.feifan.name, "上传了:", (_a = req.file) === null || _a === void 0 ? void 0 : _a.originalname, "位置:", req.query.fileurl);
                res.cookie("feifan", {
                    name: req.signedCookies.feifan.name,
                    id: req.signedCookies.feifan.id,
                    imgUrl: req.query.fileurl,
                }, { maxAge: config_1.tokenTime * 1000, httpOnly: false, signed: true });
                yield ImgSer_1.default.insert(req.query.fileurl);
                (0, infoerr_1.info)(res, "加密成功", { url: req.query.fileurl });
            }
            catch (error) {
                next(error);
            }
        });
    },
    getuserlist(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const result = yield UserSer_1.default.getuserlist();
                return (0, infoerr_1.info)(res, "ok", result);
            }
            catch (error) {
                next(error);
            }
        });
    },
    changeright(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                console.log(req.body.id, req.body.right);
                if (req.body.id &&
                    !Object.is(+req.body.id, NaN) &&
                    (req.body.right == "2" || req.body.right == "3")) {
                    const id = +req.body.id;
                    const data = { right: String(req.body.right) };
                    const result = yield UserSer_1.default.changeright(id, data);
                    if (!result)
                        return (0, infoerr_1.err)(res, "no");
                    return (0, infoerr_1.info)(res, "ok");
                }
                else
                    return (0, infoerr_1.err)(res, "参数有误", req.body);
            }
            catch (error) {
                next(error);
            }
        });
    },
};
